package com.zzhua.cglib;

import com.zzhua.cglib.callback.Callback_0;
import com.zzhua.cglib.callback.Callback_1;
import com.zzhua.cglib.callback.Callback_2;
import net.sf.cglib.proxy.Callback;
import net.sf.cglib.proxy.CallbackFilter;
import net.sf.cglib.proxy.Enhancer;

import java.lang.reflect.Method;

public class Test01 {
    public static void main(String[] args) {

        Enhancer enhancer = new Enhancer();

        // 设置callbackFilter
        enhancer.setCallbackFilter(new CallbackFilter() {
            @Override
            public int accept(Method method) {
                if ("eat".equals(method.getName())) {
                    return 0;
                } else if ("sleep".equals(method.getName())){
                    return 1;
                }
                return 2;
            }
        });

        Callback[] callbacks = new Callback[3];

        callbacks[0] = new Callback_0();
        callbacks[1] = new Callback_1();
        callbacks[2] = new Callback_2();

        Callback callbacks0 = callbacks[0];
        Callback callbacks1 = callbacks[1];
        Callback callbacks2 = callbacks[2];

        enhancer.setCallbacks(callbacks);

        // 设置目标类
        enhancer.setSuperclass(Cat.class);

//        enhancer.setStrategy();
//        enhancer.setNamingPolicy();
//        enhancer.setCallback();

        Cat cat = (Cat) enhancer.create();

        cat.eat();
        cat.sleep(1);


    }
}
